package com.tjc.cloud.util.json.ext

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.PropertyNamingStrategy

val mapper =
    ObjectMapper()

val snakeMapper: ObjectMapper = run {
    val mapper = ObjectMapper()
    mapper.propertyNamingStrategy = PropertyNamingStrategy.SNAKE_CASE
    mapper
}


inline fun Any.toJson(): String? = mapper.writeValueAsString(this)
fun Any.toSnakeJson(): String? = snakeMapper.writeValueAsString(this)

inline fun <reified T> String.toModel(): T? = this.toModel(T::class.java)
inline fun <reified T> String.toListModel(): List<T>? = this.toListModel(T::class.java)
fun <T> String.toModel(clazz: Class<T>): T? = mapper.readValue<T>(this, clazz)
fun <T> String.toListModel(clazz: Class<T>): List<T>? =
    mapper.readValue(this, mapper.typeFactory.constructParametricType(List::class.java, clazz))